﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Media;
using System.Windows.Data;
using System.Data.OracleClient;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using DSTWBQILIST;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.FTPClient;
using ReflectionStudio.Core.Helpers;
using System.Speech;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for DSTWBQILIST.xaml
    /// </summary>
    public partial class DSTWBQILIST : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        DSTWBQILISTData m_Data, m_DataX;  
        public CollectionViewSource vwTXTKON
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTXTKON");
            }
        }
        public DataRow dwTXTKON
        {
            get
            {
                if (vwTXTKON.View != null)
                {
                    return ((DataRowView)vwTXTKON.View.CurrentItem).Row;
                }
                return null;
            }
        }
        public CollectionViewSource vwTBPACK
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTBPACK");
            }
        }
        public CollectionViewSource vwVKPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwVKPOS");
            }
        }
        public DataRow dwVKPOS
        {
            get
            {
                if (vwVKPOS.View != null)
                {
                    if (vwVKPOS.View.CurrentItem != null)
                    {
                        return ((DataRowView)vwVKPOS.View.CurrentItem).Row;
                    }
                }
                return null;
            }
        }
        public SETDSTWBQILIST setUI
        {
            get
            {
                return (SETDSTWBQILIST)this.TryFindResource("SETUI");
            }
        } 
        string WkDate
        {
            get
            {
                return m_Data.Left(txtWKDATE.Text.ToString().Trim() + "                                ", 8);
            }
        }
        DataTable tbWBBST
        {
            get
            {
                return setUI.WBBST;
            }
        }
        DataTable TBPack
        {
            get
            {
                return setUI.TBPACK;
            }
        }
        public DataRow dwVVLIEFSCH;
        public DataRow dwCOLOR;
        public DataRow dwWBBST;
        public DataSet dsParamaters;
        DSTWBQILISTReport m_rpt; 
        #endregion
        public DSTWBQILIST()
        {
            InitializeComponent();
            this.Name = "成品检验报表";
        }
        string today = "20121205";
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            m_Data = new DSTWBQILISTData(dicParamater);
            m_Data.OracleDB = OracleDB;
            m_Data.proxy = proxy; 
            today = System.DateTime.Now.Year.ToString() + m_Data.Right("00" 
                + System.DateTime.Now.Month.ToString(), 2) 
                + m_Data.Right("00" + System.DateTime.Now.Day.ToString(), 2);
            txtWKDATE.Text = today;
        }  
        private void GenerateReport(string PrintType)
        {
            try
            {
                m_rpt = new DSTWBQILISTReport(dicParamater);
                m_rpt.dsSource = setUI.Copy();
                m_rpt.PrintType = PrintType;
                m_rpt.ftpClient = ftpClient;
                if (m_Data.exAgent._app == null)
                {
                    m_Data.exAgent = new ExcelX.ExcelLib.ExcelAgent(dicParamater);
                }
                m_rpt.proxy = this.proxy;
                m_rpt.OracleDB = this.OracleDB;
                m_rpt.exAgent = m_Data.exAgent; 
                m_rpt.dsSource.Clear();
                m_rpt.dsSource.Merge(setUI.Tables["WBBST"]);
                m_rpt.dsSource.Merge(m_Data.GetWBFEHL(WkDate));
                m_rpt.WriteExcelReport("EXCEL"); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using(new LongOperation(this,""))
                {
                    string PrintType = "OpenExcel";
                    GenerateReport(PrintType);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (new LongOperation(this, ""))
                {
                    setUI.Clear();
                    setUI.Merge(m_Data.GetWBBST(WkDate));
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
